package com.google.gwt.sample.hello.client.notgenerated.eventFramework.controler;

import com.google.gwt.sample.hello.client.notgenerated.eventFramework.Accessor;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.Property;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.PropertyChangeEvent;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.PropertyChangeListener;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;

@SuppressWarnings("deprecation")
public class BooleanToCheckbox implements PropertyChangeListener<Boolean>,
		ClickListener, KeyboardListener {

	private CheckBox box;
	Accessor<Boolean> accessor;

	public BooleanToCheckbox(CheckBox b, Property<Boolean> p) {
		this.accessor = p.getAccessor();
		this.box = b;
		box.setChecked(accessor.get());
		b.addClickListener(this);
		b.addKeyboardListener(this);
		p.addPropertyChangeListener(this);
		

	}


	public void propertyChanged(PropertyChangeEvent<Boolean> evt) {
		if (!(box.isChecked() == evt.getNewValue().booleanValue())) {
			box.setChecked(evt.getNewValue());
		}
	}

	public void onKeyDown(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		accessor.set(box.isChecked());
	}


	public void onClick(Widget sender) {
		accessor.set(box.isChecked());
	}

}
